﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SB;

namespace 养老金支付系统
{
    public partial class FormJYSBLR : FormBase
    {
        public FormJYSBLR()
        {
            InitializeComponent();
        }

        string grbh = string.Empty;
        string zfbh = string.Empty;
        int sbyy = 0;
        bool falg = false;//交易状态标志

        void init()
        {
            this.Text = "交易失败录入";
            textBox1.Text = SqlHelper.getScalar("select dbo.GetNowQH()") as string;
            textBox2.Text = "";
            comboBox1.DisplayMember = "显示";
            comboBox1.DataSource = SqlHelper.getDataTable("select cast(失败原因码 as char(2))+' '+失败原因 as 显示 from 失败原因");
            grbh = string.Empty;
            zfbh = string.Empty;
            sbyy = 0;
            falg = false;
            button1.Enabled = false;
        }

        void add()
        {
            SqlParameter[] p = new SqlParameter[] { new SqlParameter("@ZFBH", this.zfbh), new SqlParameter("@SBYY", this.sbyy) };
            if (SqlHelper.exeProcReturn("P_交易失败录入", p) == 1)
                MessageBox.Show(this, "添加成功！", "添加", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(this, "添加失败！", "添加", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        void del()
        {
            if (SqlHelper.exeProcReturn("P_交易失败删除", new SqlParameter("@ZFBH", this.zfbh)) == 1)
                MessageBox.Show(this, "删除成功！", "删除", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(this, "删除失败！", "删除", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        void find()
        {
            if (this.grbh.Length == 10)
            {
                const string sql = @"select
支付编号,
cast(isnull(交易状态码,1) as bit) as 交易状态码,
个人编号,
姓名,
身份证号,
支付类型,
支付金额,
银行名称,
银行户名,
银行帐号,
交易状态,
失败原因,
失败录入时间 as 录入时间
 from 查支付明细 
where 支付期号=dbo.GetNowQH() and 个人编号=@GRBH
order by 支付编号";
                SqlParameter p = new SqlParameter("@GRBH", this.grbh);
                dataGridView1.DataSource = SqlHelper.getDataTable(sql, p);
                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].Visible = false;
                /*foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (bool.Parse(row.Cells[1].Value.ToString()))
                        row.Cells[11].Style.ForeColor = Color.Green;
                    else
                        row.Cells[11].Style.ForeColor = Color.Red;
                }*/
            }
        }

        private void FormJYSBLR_Load(object sender, EventArgs e)
        {
            init();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.zfbh != string.Empty)
            {
                if (falg)//交易中
                    add();
                else
                    del();
                find();
            }
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                this.falg = bool.Parse(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
                if (this.falg) //交易中
                {
                    button1.Text = "添  加";
                }
                else
                {
                    button1.Text = "删  除";
                }
                this.zfbh = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                button1.Enabled = true;
            }
            else
                button1.Enabled = false;
        }

        private void buttonGRBH_Click(object sender, EventArgs e)
        {
            FormGRBHXZ fgrbhxz = new FormGRBHXZ();
            if (fgrbhxz.ShowDialog() == DialogResult.OK)
            {
                this.grbh = fgrbhxz.GRBH;
                textBox2.Text = this.grbh;
                find();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.sbyy = comboBox1.SelectedIndex;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.grbh = textBox2.Text;
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            find();
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                find();
        }
    }
}
